package com.jinlejia.erp.ui.common;

import android.content.Context;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jinlejia.erp.R;


/**
 * 带图标的toast
 */
public class IconToast {
    private static Toast mToast;
    private static String msg="";
    private static Handler mHandler = new Handler();
    private static Runnable r = new Runnable() {
        public void run() {
            mToast.cancel();
            msg="";
        }
    };

    public static void showToast(Context mContext,int icon, String text, int duration) {
        View layout = LayoutInflater.from(mContext).inflate(R.layout.icon_toast_layout,null);
        TextView textView = (TextView) layout.findViewById(R.id.message);
        ImageView iconIV = (ImageView) layout.findViewById(R.id.icon);
        if( !msg.equals(text)  ) {
            mHandler.removeCallbacks(r);
            if (mToast == null)
                mToast = new Toast(mContext);
                textView.setText(text);
            iconIV.setImageResource(icon);
            mHandler.postDelayed(r, duration);
            msg = text;
            mToast.setView(layout);
            mToast.setGravity(Gravity.CENTER,0,0);
            mToast.show();
        }
    }

}
